package cn.techaction.service.Impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.net.daytime.DaytimeTCPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.techaction.common.SverResponse;
import cn.techaction.dao.ActionAddressDao;
import cn.techaction.pojo.ActionAddress;
import cn.techaction.service.ActionAddressService;
import cn.techaction.utils.ConstUtil;
import cn.techaction.utils.MD5Util;

@Service
public class ActionAddressServiceImpl implements ActionAddressService {
	@Autowired
	private ActionAddressDao aAddressDao;

	@Override
	public SverResponse<String> addAddress(ActionAddress address) {
		// TODO Auto-generated method stub
		// 判断参数
		if (address == null) {
			return SverResponse.createByErrorMessage("参数错误！");
		}
		// 判断已有地址是否含有默认地址，如果没有则本条地址为默认地址，否则为一般地址
		int count = aAddressDao.findDefaultAddrByUserId(address.getUid());
		if (count == 0) {
			address.setDfault(1);
		} else {
			address.setDfault(0);
		}
		address.setCreated(new Date());
		address.setUpdated(new Date());
		address.setIsDel(0);
		int rs = aAddressDao.insertAddress(address);
		if (rs > 0) {
			return SverResponse.createRespBySuccessMessage("地址新增成功");
		}
		return SverResponse.createByErrorMessage("地址新增失败！");
	}

	@Override
	public SverResponse<String> updateAddress(ActionAddress address) {
		// TODO Auto-generated method stub
		// 判断参数
		if (address == null) {
			return SverResponse.createByErrorMessage("参数错误！");
		}
		address.setUpdated(new Date());
		int rs=aAddressDao.updateAddress(address);
		if (rs>0) {
			return SverResponse.createRespBySuccessMessage("更新成功");
		}
		return SverResponse.createByErrorMessage("地址更新失败");
	}
	@Override
	public SverResponse<String> delAddress(Integer userId, Integer id) {
		// TODO Auto-generated method stub
		//判断参数
		if (id==null) {
			return SverResponse.createByErrorMessage("参数错误");
		}
		//删除地址，并对del_state字段做更新
		ActionAddress address=new ActionAddress();
		address.setId(id);
		address.setIsDel(1);
		address.setUpdated(new Date());
		int rs=aAddressDao.updateAddress(address);
		if (rs>0) {
			return SverResponse.createRespBySuccessMessage("地址删除成功");
		}
		return SverResponse.createByErrorMessage("地址删除失败");
	}
	@Override
	public SverResponse<List<ActionAddress>> findAddrsByUserId(Integer userId) {
		// TODO Auto-generated method stub
		if (userId==null) {
			return SverResponse.createByErrorMessage("参数错误！");
		}
		List<ActionAddress> list=aAddressDao.findAddrsByUserId(userId);
		return SverResponse.createRespBySuccess(list);
	}

	@Override
	public SverResponse<String> updateaddrDefaultStatus(Integer userId, Integer id) {
		// TODO Auto-generated method stub
		if (id==null || userId==null) {
			return SverResponse.createByErrorMessage("参数错误");
		}
		//读取原来默认的地址
		ActionAddress oldAddr=aAddressDao.findDefaultAddr(userId);
		if (oldAddr!=null) {
			//取消默认地址
			oldAddr.setDfault(0);
			oldAddr.setUpdated(new Date());
			if (aAddressDao.updateAddress(oldAddr)<=0) {
				return SverResponse.createByErrorMessage("默认地址设置失败");
			}
		}
			//设置新的默认地址
			ActionAddress newaddr=new ActionAddress();
			newaddr.setDfault(1);
			newaddr.setId(id);
			newaddr.setUpdated(new Date());
			if (aAddressDao.updateAddress(newaddr)<=0) {
				return SverResponse.createByErrorMessage("默认地址设置失败");
			}
			return SverResponse.createRespBySuccessMessage("默认地址设置成功");
			
		
	}

	@Override
	public SverResponse<ActionAddress> findAddrById(Integer addrid) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				if (addrid == null) {
					return SverResponse.createByErrorMessage("参数错误！");
				}
				// 判断已有地址是否含有默认地址，如果没有则本条地址为默认地址，否则为一般地址
				ActionAddress address= aAddressDao.findAddrById(addrid);
				return SverResponse.createRespBySuccess(address);
		
		
	}

}
